<?php
use OSS\OssClient;
use OSS\Core\OssException;

class OssController extends Controller
{
    public function getImageTokenAction() {
        $key = Configure::get('oss.key');
        $secret= Configure::get('oss.secret');
        $host = Configure::get('oss.host');
        $point = Configure::get('oss.point');
        $now = time();
        $expire = 1800;
        $end = $now + $expire;
        $expiration = $this->gmt_iso8601($end);
        $dir = Configure::get('oss.imageDir').Text::randString(2).'/'.Text::randString(2).'/';

        //最大文件大小.用户可以自己设置
        $condition = array(0=>'content-length-range', 1=>0, 2=>20485760);  //最大20M
        $conditions[] = $condition; 
        //表示用户上传的数据,必须是以$dir开始, 不然上传会失败,这一步不是必须项,只是为了安全起见,防止用户通过policy上传到别人的目录
        $start = array(0=>'starts-with', 1=>'$key', 2=>$dir);
        $conditions[] = $start;
        //设置条件
        $arr = array('expiration'=>$expiration, 'conditions'=>$conditions);
        $policy = json_encode($arr);
        $base64Policy = base64_encode($policy);
        $stringToSign = $base64Policy;
        $signature = base64_encode(hash_hmac('sha1', $stringToSign, $secret, true));

        $response = array();
        $response['accessid'] = $key;
        $response['host'] = $point;
        $response['domain'] = $host;
        $response['policy'] = $base64Policy;
        $response['signature'] = $signature;
        $response['expire'] = $end;
        $response['dir'] = $dir;
        Response::ajax(200, 'success', $response);
    }
    
    public function getVideoTokenAction() {
        $key = Configure::get('oss.key');
        $secret= Configure::get('oss.secret');
        $host = Configure::get('oss.host');
        $point = Configure::get('oss.point');
        $now = time();
        $expire = 1800;
        $end = $now + $expire;
        $expiration = $this->gmt_iso8601($end);
        $dir = Configure::get('oss.videoDir').Text::randString(2).'/'.Text::randString(2).'/';

        //最大文件大小.用户可以自己设置
        $condition = array(0=>'content-length-range', 1=>0, 2=>1048576000);  //最大100M
        $conditions[] = $condition; 
        //表示用户上传的数据,必须是以$dir开始, 不然上传会失败,这一步不是必须项,只是为了安全起见,防止用户通过policy上传到别人的目录
        $start = array(0=>'starts-with', 1=>'$key', 2=>$dir);
        $conditions[] = $start;
        //设置条件
        $arr = array('expiration'=>$expiration, 'conditions'=>$conditions);
        $policy = json_encode($arr);
        $base64Policy = base64_encode($policy);
        $stringToSign = $base64Policy;
        $signature = base64_encode(hash_hmac('sha1', $stringToSign, $secret, true));

        $response = array();
        $response['accessid'] = $key;
        $response['host'] = $point;
        $response['domain'] = $host;
        $response['policy'] = $base64Policy;
        $response['signature'] = $signature;
        $response['expire'] = $end;
        $response['dir'] = $dir;
        Response::ajax(200, 'success', $response);
    }
    
    public function uploadTestAction() {        
        $ossClient = new OssClient(Configure::get('oss.key'), 
        Configure::get('oss.secret'), Configure::get('oss.point'), false);
//        $acl = OssClient::OSS_ACL_TYPE_PUBLIC_READ;
//        $ossClient->createBucket(Configure::get('oss.bucket'), $acl);
        $result = $ossClient->uploadFile(Configure::get('oss.bucket'), "image/video", APP_ROOT."/video.mp4");
        Response::dump($result);
    }
    
    private function gmt_iso8601($time) {
        $dtStr = date("c", $time);
        $mydatetime = new DateTime($dtStr);
        $expiration = $mydatetime->format(DateTime::ISO8601);
        $pos = strpos($expiration, '+');
        $expiration = substr($expiration, 0, $pos);
        return $expiration."Z";
    }
}